<?php

namespace common\models;

use common\helpers\TransformApi;
use Yii;

/**
 * This is the model class for table "{{%goods_spec}}".
 *
 * @property int $id
 * @property int|null $g_id 商品id
 * @property string|null $name 名称
 * @property float|null $price 价格
 * @property int|null $stock 库存
 * @property int|null $surplus 剩余
 */
class GoodsSpec extends BaseModel
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return '{{%goods_spec}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['g_id', 'stock','surplus'], 'integer'],
            [['price'], 'number'],
            [['name'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'g_id' => '商品id',
            'name' => '名称',
            'price' => '价格',
            'stock' => '库存',
            'surplus' => '剩余',
        ];
    }

    public function getSpec($g_id,$is_huiyuan,$discount)
    {
        $discount = $discount??0;
        $data = self::find()->where(['g_id'=>$g_id])->select(['id as spec_id','name','price','surplus'])->asArray()->all();
        foreach ($data as &$datum){
            if ($discount>0&&$is_huiyuan){
                $datum['price'] = $datum['price']*($discount/10);
                $datum['price'] = TransformApi::decimalPoint($datum['price']);
            }
        }
        return $data;
    }
}
